<?php
require $_SERVER['DOCUMENT_ROOT'].'/../_core/common.php';

//权限验证
if( Role::init() == false ){
	exit('没有权限');
}

require $_SERVER['DOCUMENT_ROOT'].'/../_interface/category.php';

$db = new DB('news');

$edit_uid = Sessions::get('uid');
$updated = time();
$title = isset($_POST['title']) ? h($_POST['title']) : '';
$title_py = isset($_POST['title_py']) ? h($_POST['title_py']) : '';
$title_en = isset($_POST['title_en']) ? h($_POST['title_en']) : '';
$seo_keyword = isset($_POST['seo_keyword']) ? h($_POST['seo_keyword']) : '';
$seo_description = isset($_POST['seo_description']) ? h($_POST['seo_description']) : '';
$orders = isset($_POST['orders']) ? intval($_POST['orders']) : 0;
$url = isset($_POST['url']) ? h($_POST['url']) : '';
$dirs = isset($_POST['dirs']) ? h($_POST['dirs']) : '';
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
$parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : '';

if($id <= 0 || $title == '' ){
	echo '<script>alert("参数有误！");window.history.go(-1);</script>';
	exit;
}

//判断名称是否重复
if($parent_id !== ''){//栏目层级改变了
	$category_sql = 'select id,title from categories where title="'.$title.'" and parent_id='.$parent_id.' and is_del=1 limit 1';
	$rs = $db->query($category_sql)->getOne();

	if($rs['id']==true && $rs['id']!=$id){
		echo '<script>alert("修改失败，栏目《'.$title.'》已存在！");window.history.go(-1);</script>';
		exit;
	}
}else{
	$category_sql = 'select parent_id from categories where id='.$id.' and is_del=1 limit 1';
	$rs = $db->query($category_sql)->getOne();
	$category_sql = 'select id,title from categories where title="'.$title.'" and parent_id='.$rs['parent_id'].' and is_del=1 limit 1';
	$rs = $db->query($category_sql)->getOne();

	if($rs['id']==true && $rs['id']!=$id){
		echo '<script>alert("修改失败，栏目《'.$title.'》已存在！");window.history.go(-1);</script>';
		exit;
	}
}

//更新关联字段child_id、parent_id、top_id
if($parent_id !== ''){//栏目层级改变了
	$category = new Category($db);
	$category_sql = 'select child_id,title from categories where id='.$id.' and is_del=1 limit 1';
	$rs = $db->query($category_sql)->getOne();
	
	//更新child_id
	if($rs['child_id'] == ''){
		$child_id = $id;
	}else{
		$child_id = $id.','.$rs['child_id'];
	}
	
	//更新当前id记录的原来父级的child_id字段
	$child_array = explode(',',$child_id);
	foreach($child_array as $child){
		$category -> child_del($id,$child);
	}
	
	//更新当前id记录的parent_id
	$data = array('parent_id'=>$parent_id);
	$db->table('categories')->where('id='.$id)->update($data)->run();
	
	//把当前id记录的child_id加到新的父级里
	if($parent_id != 0){
		foreach($child_array as $child){
			$category -> child_add($id,$child);
		}
	}
	
	//更新当前id记录的top_id
	if($parent_id == 0){
		$top_id = 0;
	}else{
		$category_sql = 'select id,top_id from categories where id='.$parent_id.' and is_del=1 limit 1';
		$parent_rs = $db->query($category_sql)->getOne();
		if($parent_rs['top_id'] == 0){
			$top_id = intval($parent_rs['id']);
		}else{
			$top_id = intval($parent_rs['top_id']);
		}
	}
	$data = array('top_id'=>$top_id);
	$db->table('categories')->where('id='.$id)->update($data)->run();
	
	
	//更新子级栏目的top_id
	if($parent_id == 0){
		$top_id = $id;
	}else{
		$category_sql = 'select id,top_id from categories where id='.$parent_id.' and is_del=1 limit 1';
		$parent_rs = $db->query($category_sql)->getOne();
		if($parent_rs['top_id'] == 0){
			$top_id = intval($parent_rs['id']);
		}else{
			$top_id = intval($parent_rs['top_id']);
		}
	}
	if($rs['child_id'] != ''){
		$child_array = explode(',',$rs['child_id']);
		foreach($child_array as $child){
			$data = array('top_id'=>$top_id);
			$db->table('categories')->where('id='.$child)->update($data)->run();
		}
	}
}

//更新其他字段
$data=array('title'=>$title,
			'title_en'=>$title_en,
			'title_py'=>$title_py,
			'seo_keyword'=>$seo_keyword,
			'seo_description'=>$seo_description,
			'url'=>$url,
			'dirs'=>$dirs,
			'orders'=>$orders,
			'updated'=>$updated,
			'edit_uid'=>$edit_uid,
			);
$category = $db->table('categories')->where('id='.$id)->update($data)->run();


echo '<script>alert("修改成功!");window.location.href="/www/admin/news/category/category_lists.php";</script>';
exit;